package com.surent.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hailong.surent.R;

public class TextBaseAdapter extends BaseAdapter {

	private Context context;

	private String[] strData;

	private char[] chaTitle;

	private LayoutInflater layoutInflater;

	public TextBaseAdapter(Context context, String[] strData, char[] chaTitle) {
		this.context = context;
		this.strData = strData;
		this.chaTitle = chaTitle;
		layoutInflater = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {
		return strData.length;
	}

	@Override
	public Object getItem(int position) {
		return strData[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		MyTag mytag = null;

		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.item_indexlistview, null);
			mytag = new MyTag();
			mytag.text_char = (TextView) convertView.findViewById(R.id.text_char);
			mytag.text_text = (TextView) convertView.findViewById(R.id.text_text);
			convertView.setTag(mytag);
		} else {
			mytag = (MyTag) convertView.getTag();
		}

		String text = strData[position];
		mytag.text_text.setText(text);

		char cha = chaTitle[position];

		if (position > 0) {
			char cha2 = chaTitle[position - 1];
			if (cha == cha2) {
				mytag.text_char.setVisibility(View.GONE);
			} else {
				mytag.text_char.setText(cha + "");
				mytag.text_char.setVisibility(View.VISIBLE);
			}
		} else {
			mytag.text_char.setText(cha + "");
			mytag.text_char.setVisibility(View.VISIBLE);
		}

		return convertView;
	}

	/**
	 * ��ͼ��ǩ��
	 * 
	 * @author Hasee
	 * 
	 */
	public class MyTag {

		TextView text_char, text_text;

	}

}
